# Teno计算中心
print('------')
from mpython import *
import Teno_Brain.dice as D
from Teno_Brain.SPoF import *
import network
import ntptime
import time
import urequests
import ujson
import random
import gc
print('RSaD.py')

gc.enable()

sig_toh_ = 'PY'
val_but_ = 'Null'

def f_tme_():
    global tme_mon_, tme_dat_, tme_hou_, tme_min_
    if 'WiFi' in DIV_sta_:
        if DIV_sta_['WiFi'] == 1:  # 判断WiFi是否已连接
            try:
                ntptime.settime(8, "time.windows.com")  # 同步网络时间
                tme_mon_ = time.localtime()[1]
                tme_dat_ = time.localtime()[2]
                tme_hou_ = time.localtime()[3]
                tme_min_ = time.localtime()[4]
            except Exception as e:
                tme_mon_ = time.localtime()[1]
                tme_dat_ = time.localtime()[2]
                tme_hou_ = time.localtime()[3]
                tme_min_ = time.localtime()[4]
        elif DIV_sta_['WiFi'] == 0:  # 如果WiFi未连接
            tme_mon_ = time.localtime()[1]
            tme_dat_ = time.localtime()[2]
            tme_hou_ = time.localtime()[3]
            tme_min_ = time.localtime()[4]
            wii_con_(1)
        else:
            pass
    else:
        pass

def f_wer_():
    _url = _url + "&location=" + _location.replace(" ", "%20")
    try:
        # 发送GET请求获取天气数据
        response = urequests.get(_url)
        json = response.json()
        response.close()
        return json
        wer_1_ = get_seni_weather("https://api.seniverse.com/v3/weather/now.json?key=SI9mELqugg0ma-J0v", "ip")
    except:
        D.fresh_(0)
        D.text_line_('天气获取失败','看看窗外吧')
        D.fresh_(1)
        return None

def f_lit_():
    vae_lit_ = light.read()
    return vae_lit_

def f_sod_():
    vae_sou_ = sound.read()
    return vae_sou_

def f_rad_(min=0, max=1, mode=0):
    if mode == 0:
        val_ran_ = random.randint(min, max)
    elif mode == 1:
        val_ran_ = random.uniform(min, max)

def f_toh_():
    global sig_tou_
    sig_tou_ = 'Null'
    if touchpad_p.is_pressed() and touchpad_y.is_pressed():
        sig_tou_ = 'PY'
    elif touchpad_t.is_pressed() and touchpad_h.is_pressed():
        sig_tou_ = 'TH'
    elif touchpad_o.is_pressed() and touchpad_n.is_pressed():
        sig_tou_ = 'ON'

def f_but_():
    global val_but_
    val_but_ = 'Null'
    if button_a.value() == 0:
        val_but_ = 'A'
    elif button_b.value() == 0:
        val_but_ = 'B'
    elif button_a.value() == 0 and button_b.value() == 0:
        val_but_ = 'AB'

def f_mat_():
    val_mat_ = sum(magnetic.get_xyz()) // 3
    return val_mat

def f_cos_():
    val_cos_ = int(magnetic.get_heading())
    return val_cos_

def f_fit_(file_url_):
    global file_content_
    if 'WiFi' in DIV_sta_:
        if DIV_sta_['WiFi'] == 1:  # 判断WiFi是否已连接
            try:
                # 发送 HTTP GET 请求
                response = urequests.get(file_url_)
                if response.status_code == 200:
                    file_content_ = response.text
                else:
                    D.fresh_(0)
                    D.text_line_('获取文件失败')
                    D.fresh_(1)
                response.close()
            except Exception as e:
                D.fresh_(0)
                D.text_line_('获取文件失败')
                D.fresh_(1)
        else:  # 如果WiFi未连接
            D.fresh_(0)
            D.text_line_('请稍等', '正在联网')  # 显示提示信息
            D.fresh_(1)
            wii_con_(1)  # 连接WiFi
    else:
        D.fresh_(0)
        D.text_line_('网络被Senior OS抢走啦！')
        D.fresh_(1)

def f_por_(): #   剩余电量
    try:
        bay_rvol_ = 3.3
        bay_cvol_ = parrot.get_battery_level()
        bay_rem = (bay_cvol_ / 1000) / bay_rvol_ * 100
        return str(bay_rem)
    except:
        pass

def f_gry_(mode=1, mass=0): #   物理投射计算
    global gry_res_
    if mode == 1:
        gry_res_ = mass*9.8
    else:
        pass
        